/*!
    \copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup     gmap_profile
    \brief
*/

#include "gmap_server_role_advertising.h"
#include "gatt_gmas_server_uuids.h"
#include "gatt_gmas_server.h"
#include "lea_advertising_policy.h"

#include <logging.h>
#include <panic.h>
#include <stdlib.h>

#define GMAP_SERVER_SIZE_ADVERT                  5

/*! Service Data (16-bit default). */
#define GMAP_BLE_AD_TYPE_SERVICE_DATA            0x16

#ifdef INCLUDE_LE_AUDIO_UNICAST
#define GMAP_UNICAST_ROLE                        GMAS_ROLE_UNICAST_GAME_TERMINAL
#else
#define GMAP_UNICAST_ROLE                        0
#endif /* INCLUDE_LE_AUDIO_UNICAST */

#ifdef INCLUDE_LE_AUDIO_BROADCAST
#define GMAP_BROADCAST_ROLE                      GMAS_ROLE_BROADCAST_GAME_RECEIVER
#else
#define GMAP_BROADCAST_ROLE                      0
#endif

/*! Gmap supporting role characteristic valus (UGT, BGR))*/
#define GMAP_ROLE_VALUE                          (GMAP_UNICAST_ROLE | GMAP_BROADCAST_ROLE)

#define GMAP_DEBUG_INFO                          DEBUG_LOG

static const uint8 gmapServerAdvertData[GMAP_SERVER_SIZE_ADVERT] = {
    GMAP_SERVER_SIZE_ADVERT - 1,
    GMAP_BLE_AD_TYPE_SERVICE_DATA,
    /* GMAS UUID */
    UUID_GAMING_AUDIO_SERVICE & 0xFF,
    UUID_GAMING_AUDIO_SERVICE >> 8,
    /* GMAP ROLE*/
    (GMAP_ROLE_VALUE) & 0xFF,
};

static uint8 gmapServer_GetAdvertisingDataSize(void)
{
    return GMAP_SERVER_SIZE_ADVERT;
}

static uint8 gmapServer_GetAdvertisingData(const lea_adv_policy_adv_param_t *params,
                                           uint8 *advert_buffer,
                                           uint8 advert_buffer_length)
{
    UNUSED(params);
    DEBUG_LOG("gmap_GetAdvertisingData");
    PanicFalse(advert_buffer_length >= GMAP_SERVER_SIZE_ADVERT);

    memcpy(advert_buffer, gmapServerAdvertData, GMAP_SERVER_SIZE_ADVERT);
    return GMAP_SERVER_SIZE_ADVERT;
}

static const lea_adv_policy_clients_callback_t gmap_server_callback =
{
    .GetAdvertisingDataSize = gmapServer_GetAdvertisingDataSize,
    .GetAdvertisingData = gmapServer_GetAdvertisingData
};

bool GmapServer_SetupLeAdvertisingData(void)
{
    DEBUG_LOG("GmapServer_SetupLeAdvertisingData - Registering with LE Advert Policy");
    LeaAdvertisingPolicy_RegisterClient(&gmap_server_callback);
    return TRUE;
}
